"""
Case Type   : cm
Case Name   : AZ内节点故障时，cm_ctl switchover操作指令执行AZ级别切换
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、停止AZ2内的节点
    3、执行cm_ctl switchover切换到已故障的AZ
    4、恢复集群状态
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、stop成功
    3、switchover切换失败，合理报错
    4、恢复集群状态成功
History     :
"""

import os
import re
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(5 != Primary_SH.get_node_num(), '需1主4备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_cm(self):
        text = '----step1:查看集群状态，查询AZ分布; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv', '-z ALL')
        self.log.info(status)
        AZ_status = status.splitlines()[-1]
        self.log.info(AZ_status)
        self.assertTrue('AZ1' in status and 'AZ2' in status, '集群部署方式不对')

        text = '----step2:执行cm_ctl stop掉AZ2节点; expect:stop成功----'
        self.log.info(text)

        stop = self.comsh.exec_cm_ctl('stop', '-z AZ2')
        self.log.info(stop)
        self.assertIn('stop availability zone successfully', stop, '停止失败')

        text = '------step3:执行cm_ctl switchover -n nodeid; expect:合理报错------'
        self.log.info(text)

        switch = self.comsh.exec_cm_ctl('switchover', '-z AZ2')
        self.log.info(switch)
        self.assertIn('fail', switch, '切换成功')

    def tearDown(self):
        text = '------step4:恢复集群状态; expect:恢复集群状态成功------'
        self.log.info(text)

        start = self.comsh.exec_cm_ctl('start')
        self.log.info(start)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        p_status = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        if 'Primary' in p_status:
            self.log.info('---集群状态正常，无需恢复为初始状态---')
        else:
            recovery = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
            self.log.info(recovery)

            self.assertIn(self.constant.cm_switchover_success_msg, recovery,
                          '切换失败')

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '状态异常')
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '状态异常')
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '状态异常')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
